package com.edu.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.demo.entity.Department;
import com.edu.demo.exception.MyNullPointerException;
import com.edu.demo.pojo.AjaxResult;
import com.edu.demo.service.IDepartmentService;
import com.edu.demo.service.impl.DepartmentServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 院系 前端控制器
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有院系
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@ps.hasPerm('sys:department:list')")
    public AjaxResult list(){
        List<Department> list = departmentService.list();
        Collections.sort(list);
        return AjaxResult.success(list);
    }

    /**
     * 批量添加学院
     * @param file
     * @return
     */
    @PostMapping("/add/all")
    @PreAuthorize("@ps.hasPerm('sys:department:add')")
    public AjaxResult addAll(@RequestParam("file") MultipartFile file){
        try {
            int i = departmentService.insertDepartments(file);
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof MyNullPointerException) {
                String message = e.getMessage();
                return AjaxResult.error(message);
            }else return AjaxResult.error("服务出错");
        }

        return AjaxResult.success();
    }

    /**
     * 单独添加学院
     * @param department
     * @return
     */
    @PostMapping("/add/one")
    @PreAuthorize("@ps.hasPerm('sys:department:add')")
    public AjaxResult addOne(@RequestBody Department department){
        if(Strings.isEmpty(department.getDeptName())||Strings.isEmpty(department.getDeptCode()))
            return AjaxResult.error("院系名称或院系编号不能为空");


        Department serviceOne = departmentService.getOne(new QueryWrapper<Department>().eq("dept_code", department.getDeptCode()));
        if(Objects.isNull(serviceOne)) {
            departmentService.save(department);

            return AjaxResult.success();
        }else {
            return AjaxResult.error("学院编号重复，请修改");
        }
    }


    /**
     * 修改更新学院
     * @param department
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("@ps.hasPerm('sys:department:add')")
    public AjaxResult update(@RequestBody Department department){
        if(Strings.isEmpty(department.getDeptName())||Strings.isEmpty(department.getDeptCode()))
            return AjaxResult.error("院系名称或院系编号不能为空");

        boolean b = departmentService.updateById(department);

        if(b)return AjaxResult.success();
        return AjaxResult.error();
    }
}
